package com.woniu.shops.controller;

import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Order;
import com.woniu.common.pojo.Shops;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.AjaxResult;
import com.woniu.common.vo.Shopsss;
import com.woniu.shops.service.CommodityService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/commodity")
@CrossOrigin
public class CommodityController {
    @Resource
    private CommodityService commodityService;

    @RequestMapping("findAll")
    public AjaxResult findAll(HttpServletRequest request, @Param("pageNum")Integer pageNum, @Param("pageSize")Integer pageSize) {
        Shops shop = TokenUtil.getObj(request, Shops.class);
        List<Flower> findll = commodityService.findll(shop);
        ArrayList<Flower> flowers = new ArrayList<>();
        Integer pageNums = (pageNum-1)*pageSize;//6
        Integer is = findll.size() - pageSize - pageNums;
        if(is < 0){
            for (int i = pageNums; i < findll.size(); i++) {
                flowers.add(findll.get(i));
            }
        }else {
            for (int i = pageNums; i < pageNums+pageSize; i++) {
                flowers.add(findll.get(i));
            }
        }
        Shopsss shopsss = new Shopsss(flowers, findll.size());
        return AjaxResult.success("店铺商品如下:", shopsss);
    }

    @RequestMapping("delete")
    public AjaxResult delete(@RequestBody Flower flower) {
        commodityService.deleteFlower(flower);
        return AjaxResult.success("删除成功");
    }

    @RequestMapping("updateCommodity")
    public AjaxResult updateCommodity(@RequestBody Flower flower) {
        commodityService.updateCommodity(flower);
        return AjaxResult.success("修改成功");
    }

    @RequestMapping("addsCommodity")
    public AjaxResult addsCommodity(@RequestBody Flower flower, HttpServletRequest request) {
        Shops shop = TokenUtil.getObj(request, Shops.class);
        flower.setShops(shop);
        commodityService.addsCommodity(flower);
        return AjaxResult.success("修改成功");
    }

    //    查询所有
    @RequestMapping("findAllFlower")
    public AjaxResult findAllFlower() {
        List<Flower> allFlower = commodityService.findll();
        for (Flower flower : allFlower) {
            System.out.println(flower);
        }
        return AjaxResult.success("查询成功", allFlower);
    }
}
